<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller{
	//登录页面
	public function getLogin(){
		//登录之后用Session来存储用户的信息  
		//若是用户已经登录  再次到登录页面的时候就让自动登录到后台首页
		if(Session::get('logininfo')){
    		$this->redirect('/admin/index');
    	}else{
    		return $this->fetch('/Login/login');
    	}
	}
	
	//执行登录
	//不能通过URl方式访问(禁止GET方式访问)
	public function getLoginin(){
		$this->error('非法操作','/login/login');
	}
	public function postLoginin(){
		$req = Request();
		if (!$req->isPost()){
			$this->error('非法操作','/login/login');
		}else{
			// echo captcha_check($req->param('fcode'));die;
			if(!captcha_check($req->param('fcode'))){
				$this->error('验证码错误','/login/login');
			}else{
				$userinfo = Db::table('admin_user')->where('username',$req->param('username'))->where('pass',md5($req->param('pass')))->find();
				if ($userinfo) {	
					Session::set('logininfo',$userinfo);
					//1.获取当前用户登录信息存储在session中
					// Db::query
					//2.初始化权限
					//3. 
					$this->success('登录成功','/admin/index');
				}else{
					$this->error('登录失败','/login/login');
				}
			}
			
		}
	}

	public function getLogout(){
		Session::set('logininfo',null);
		$this->redirect('/login/login');
	}
	//用来验证用户是否被禁止登录
	public function getYzqx(){
		$req = Request();
		$info = Db::table('admin_user')->where('username',$req->param('username'))->find();

		if ($info['status'] == 1) {
			echo 1;
		}else{
			echo 0;
		}
	}


	//锁屏
	public function getLock(){
		// if (!Session::get('logininfo.pass')) {
		// 	$this->redirect('/admin/index');
		// }
		Session::set('logininfo.pass',null);
		return $this->fetch('/login/lock');
	}
	//执行解锁
	public function postUnlock(){
		echo "string";
	}
	//解锁界面执行重新登录
	public function getLockout(){
		Session::set('logininfo',null);
		$this->redirect('/login/login');
	}
	//解锁验证密码是否正确
	public function getYzLock(){
		//v($_GET['pass']);
		$req = Request();
		// echo $req->param('pass');die;
		$a = Db::table('admin_user')->where('username','=',Session::get('logininfo.username'))->where('pass',md5($req->param('pass')))->find();
		// sql('admin_user');
		if ($a) {
			Session::set('logininfo.pass',md5($req->param('pass')));
			echo 1;
		}
	}
	
}











